package edu.gatech.cs2340.OregonTrail;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.IOException;



public class MapPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	
	//private OregonTrailApp parentFrame;
	private Trail trail;
	private Image backgroundImage;
	private Image wagon;
	private MapNode[] NodeArray = new MapNode[31];
	private MapNode currentNode;
	private int x;
	private int y;
	private int index;
	
	
	public MapPanel(final OregonTrailApp parentFrame, Trail trail) throws IOException {
		setBackground(Color.BLACK);
		//this.parentFrame = parentFrame;
		this.trail = trail;
		index = 0;
		
		//Branches have not been added yet and these points need some adjustment for aesthetics
		NodeArray[0] = new MapNode(719,289,0);
		NodeArray[1] = new MapNode(698,306,50);
		NodeArray[2] = new MapNode(683,306,102);
		NodeArray[3] = new MapNode(660,291,143);
		NodeArray[4] = new MapNode(642,285,185);
		NodeArray[5] = new MapNode(621,280,244);
		NodeArray[6] = new MapNode(600,272,304);
		NodeArray[7] = new MapNode(573,272,429);
		NodeArray[8] = new MapNode(511,260,554);
		NodeArray[9] = new MapNode(478,255,597);
		NodeArray[10] = new MapNode(455,245,640);
		NodeArray[11] = new MapNode(441,232,735);
		NodeArray[12] = new MapNode(412,232,830);
		NodeArray[13] = new MapNode(388,243,881);
		NodeArray[14] = new MapNode(356,250,932);
		NodeArray[15] = new MapNode(340,266,992);
		NodeArray[16] = new MapNode(326,278,1057);
		NodeArray[17] = new MapNode(312,250,1138);
		NodeArray[18] = new MapNode(298,232,1219);
		NodeArray[19] = new MapNode(286,228,1276);
		NodeArray[20] = new MapNode(263,236,1367);
		NodeArray[21] = new MapNode(240,236,1458);
		NodeArray[22] = new MapNode(212,222,1515);
		NodeArray[23] = new MapNode(196,206,1572);
		NodeArray[24] = new MapNode(180,184,1653);
		NodeArray[25] = new MapNode(160,160,1734);
		NodeArray[26] = new MapNode(154,118,1789);
		NodeArray[27] = new MapNode(125,122,1849);
		NodeArray[28] = new MapNode(108,126,1909);
		NodeArray[29] = new MapNode(86,130,1954);
		NodeArray[30] = new MapNode(66,126,2000);

		
		
		backgroundImage = ImageIO.read(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/otmap.png"));
		wagon = ImageIO.read(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/mapindicator.png"));

		setPreferredSize(new Dimension(756, 376));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{18, 100, 622, 0};
		gridBagLayout.rowHeights = new int[]{298, 25, 15, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JButton btnGoBack = new JButton("Go Back");
		btnGoBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parentFrame.showPlayPanel();
			}
		});
		GridBagConstraints gbc_btnGoBack = new GridBagConstraints();
		gbc_btnGoBack.insets = new Insets(0, 0, 5, 5);
		gbc_btnGoBack.gridx = 1;
		gbc_btnGoBack.gridy = 1;
		add(btnGoBack, gbc_btnGoBack);
		
	}
	
	public void updateMap(){
		while(true && index < 31){
			currentNode = NodeArray[index];
			if(currentNode.getDistance() >= trail.getMilesTraveled()){
				x = currentNode.getX();
				y = currentNode.getY();
				break;
			}
			else{
				index++;
			}
		}
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		g.drawImage(backgroundImage, 0, 0, null);
		g.drawImage(wagon, x, y, null);
	}
}